/*
 * @Author: huangyq
 * @Date: 2022-04-20 17:47:10
 * @LastEditTime: 2022-06-14 13:42:21
 * @LastEditors: huangyq
 * @Description: 缓存hooks
 * @FilePath: /vue3-boilerplate-pc-client/src/hooks/useStorage.ts
 */
import { ref, watchEffect } from 'vue'

export function useLocalStorage<T>(name: string, value: unknown) {
  const data = ref<T>(
    JSON.parse(localStorage.getItem(name) || JSON.stringify(value))
  )

  watchEffect(() => {
    localStorage.setItem(name, JSON.stringify(data.value))
  })
  return data
}
export function useSessionStorage<T>(name: string, value: unknown) {
  const data = ref<T>(
    JSON.parse(sessionStorage.getItem(name) || JSON.stringify(value))
  )
  watchEffect(() => {
    sessionStorage.setItem(name, JSON.stringify(data.value))
  })
  return data
}
